package com.guli.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.guli.common.utils.PageUtils;
import com.guli.common.utils.R;
import com.guli.product.entity.AttrEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.guli.product.entity.AttrAttrgroupRelationEntity;
import com.guli.product.service.AttrAttrgroupRelationService;


/**
 * 属性&属性分组关联
 * @author zzb
 * @email 1626131069@qq.com
 * @date 2023-10-21 10:18:56
 */
@RestController
public class AttrAttrgroupRelationController {
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;

    /**
     * 获取与该属性分组相关联的属性信息
     * @param attGroupId
     * @return
     */
    @GetMapping("product/attrgroup/{attGroupId}/attr/relation")
    public R attrGroupRelation(@PathVariable Long attGroupId) {
        List<AttrEntity> attrEntityList = attrAttrgroupRelationService.attrGroupRelation(attGroupId);
        return R.ok().put("data", attrEntityList);
    }

    /**
     * 查询没有关联该分组的所有属性，并且这些属性必须在该分类下
     * @param attrGroupId
     * @param params
     * @return
     */
    @GetMapping("product/attrgroup/{attrGroupId}/noattr/relation")
    public R attrNoRelationGroupAddAttr(@PathVariable Long attrGroupId, @RequestParam Map<String, Object> params) {
        PageUtils pages = attrAttrgroupRelationService.queryPage(attrGroupId, params);
        return R.ok().put("page", pages);
    }

    /**
     * 添加属性与属性分组关联的信息
     * @param attrgroupRelation
     * @return
     */
    @PostMapping("product/attrgroup/attr/relation")
    public R addRelation(@RequestBody AttrAttrgroupRelationEntity[] attrgroupRelation) {
        //查询是否存在attrId
        int count = attrAttrgroupRelationService.count(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", attrgroupRelation[0].getAttrId()));
        if (count == 0) {
            attrAttrgroupRelationService.saveBatch(Arrays.asList(attrgroupRelation));
            return R.ok();
        }
        attrAttrgroupRelationService.update(new UpdateWrapper<AttrAttrgroupRelationEntity>().set("attr_group_id", attrgroupRelation[0].getAttrGroupId())
                .eq("attr_id", attrgroupRelation[0].getAttrId()));
        return R.ok();
    }

    /**
     * 删除属性与属性分组关联的信息
     * @param attrgroupRelation
     * @return
     */
    @PostMapping("product/attrgroup/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrAttrgroupRelationEntity[] attrgroupRelation) {
        List<AttrAttrgroupRelationEntity> attrgroupRelationEntityList = Arrays.asList(attrgroupRelation);
        attrgroupRelationEntityList.forEach(item -> {
            AttrAttrgroupRelationEntity attr = attrAttrgroupRelationService
                    .getOne(new QueryWrapper<AttrAttrgroupRelationEntity>().eq("attr_id", item.getAttrId()));
            attrAttrgroupRelationService.updateAttr(attr.getId());
        });
        return R.ok();
    }
}
